package com.wxy.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.wxy.eneity.Connection;
import com.wxy.service.LoginService;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author 47868
 */
@Controller
@RequestMapping("/connection")
public class LoginController {
    private final LoginService loginService;
    @Autowired
    public LoginController(LoginService loginService) {
        this.loginService = loginService;
    }
    @RequestMapping("/getConnection")
    @ResponseBody
    public String getConnection(@RequestBody Connection connection, HttpServletResponse response) throws Exception {
        String message = loginService.getConnection(connection);
        if("error".equals(message)){
            return "error";
        }
        Cookie cookie = new Cookie("token", message);
        cookie.setPath("/");
        response.addCookie(cookie);
        return "success";
    }

}
